﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Ccr.Core;
using System.Reflection;
using Microsoft.Ccr.Core.Arbiters;
using XcoAppSpaces.Contracts.CcrExtensions;
using XcoAppSpaces.Contracts.CcrWiring;
using XcoAppSpaces.Core.CcrWiring.CcrRemote;

namespace XcoAppSpaces.Core.CcrWiring
{
	/// <summary>
	/// This class adds extension methods to the port that are needed at the appspace for serializing and deserializing ports,
	/// as well as registering remotable tasks.
	/// </summary>
	static class PortHelpers
	{
		/// <summary>
		/// Gets infos that are available for this port about remote communication.
		/// </summary>
		/// <param name="port">The port.</param>
		/// <returns>Remote communication information about this port.</returns>
		public static XcoPortSerializationInfo GetPortSerializationInfo(this IPort port)
		{
		    var portInfo = port.GetPortInfo();
			if (portInfo != null && portInfo.RemotePortID != Guid.Empty)
                return new XcoPortSerializationInfo(portInfo.RemoteAddress, portInfo.RemotePortID);
			return new XcoPortSerializationInfo(null, Guid.Empty);
		}

		internal static void ReceiveWithRemotableTask(this IPort port, DispatcherQueue dpq, RemotableTask task)
		{
			ReceiveWithRemotableTask(port, dpq, task, null);
		}

		internal static void ReceiveWithRemotableTask(this IPort port, DispatcherQueue dpq, RemotableTask task, Dictionary<Type, XcoProcessorAttribute> processorAttributes)
		{
			if (port is IPortSet)
			{
				foreach (var portSetPort in ((IPortSet)port).Ports)
				{
					portSetPort.ReceiveWithRemotableTask(dpq, task, processorAttributes);
				}
			}
			else
			{
				var itemCount = ((IPortReceive) port).GetReceivers().Count();
				if (itemCount == 0)
					((IPortReceive)port).RegisterReceiver(new RemotePortInfoTask(task));
				if (itemCount < 2)
					Arbiter.Activate(dpq, task.CreateReceiver(port, GetTransferMode(port, processorAttributes))); 
					//Arbiter.Activate(dpq, new RemotableReceiverTask(GetTransferMode(port, processorAttributes), port, task));
			}
		}

		private static MessageTransferMode GetTransferMode(IPort port, IDictionary<Type, XcoProcessorAttribute> processorAttributes)
		{
			if (processorAttributes == null)
				return MessageTransferMode.Sequential; //default transfer mode is sequential
			Type messageType = port.GetMessageType();
			if (!processorAttributes.ContainsKey(messageType))
				return MessageTransferMode.Sequential;
			return processorAttributes[messageType].MessageTransferMode;
		}
	}
}
